package com.flash.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.flash.constant.MessageConstant;
import com.flash.constant.StatusConstant;
import com.flash.dto.DishDTO;
import com.flash.dto.DishPageQueryDTO;
import com.flash.entity.Dish;
import com.flash.entity.DishFlavor;
import com.flash.exception.DeletionNotAllowedException;
import com.flash.exception.SetmealEnableFailedException;
import com.flash.mapper.DishFlavorMapper;
import com.flash.mapper.DishMapper;
import com.flash.mapper.SetmealDishMapper;
import com.flash.result.PageResult;
import com.flash.service.DishService;
import com.flash.utils.Output;
import com.flash.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品以及口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条
        dishMapper.insert(dish);
        Long id = dish.getId();
        //口味表插入n条
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&& !flavors.isEmpty()){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return  new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {

        //判断菜品是否能够删除,不能的话就抛出异常
        int Number = dishMapper.couldNotBeDeleted(ids);
        if (Number != 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //菜品是否被某个套餐关联
        List<Long> setMealIdsByDishIds = setmealDishMapper.getSetMealIdsByDishIds(ids);
        if (setMealIdsByDishIds != null && !setMealIdsByDishIds.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品数据
        dishMapper.deleteById(ids);
        //删除菜品表中的口味数据
        dishFlavorMapper.deleteByDishId(ids);
    }

    @Override
    public DishVO selectByIdWithFlavor(Long id) {
        Dish dish = dishMapper.selectById(id);
        List <DishFlavor> dishFlavor = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        dishFlavorMapper.deleteByDishId(Collections.singletonList(dishDTO.getId()));
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()){

            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public void updateStatus(Integer status, Long id) {
        if (status == StatusConstant.ENABLE){
            List<Dish> dishList = dishMapper.getBySetMealId(id);
            if (dishList != null && !dishList.isEmpty()){
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        dishMapper.updateDishStatus(status,id);
    }

    @Override
    public List<Dish> getDishByCategory(Long categoryId) {
        List<Dish>dishList =  dishMapper.getDishByCategory(categoryId);
        Output.Output(dishList,"service");
        return dishList;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
